/*
 * @Author: wang cong wangc9832@163.com
 * @Date: 2023-05-05 13:58:36
 * @LastEditors: wang cong wangc9832@163.com
 * @LastEditTime: 2023-05-30 20:13:23
 * @FilePath: /ams-front/src/utils/vaidator.ts
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
const formValidation = {
  num0: {
    rule: (rule, value, callback) => {
      const reg = /^[1-9]\d*$/;
      if (!reg.test(value)) {
        callback('数字格式不正确，只能为正整数');
      } else {
        callback();
      }
    },
    name: '整数(正数)',
  },
  num1: {
    rule: (rule, value, callback) => {
      const reg = /^(^-?\d+$)$/;
      if (!reg.test(value)) {
        callback('数字格式不正确，只能为整数');
      } else {
        callback();
      }
    },
    name: '整数(正数+负数)',
  },
  mobile: {
    rule: (rule, value, callback) => {
      // const reg = /^1(3|4|5|7|8)\d{9}$/;
      const reg = /^(?:(?:\+|00)86)?1\d{10}$/;
      if (!reg.test(value)) {
        callback('请输入正确的手机号');
      } else {
        callback();
      }
    },
    name: '手机号',
  },
  email: {
    rule: (rule, value, callback) => {
      const reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      if (!(reg.test(value) || Number(value) === 0)) {
        callback('请输入正确的邮箱');
      } else {
        callback();
      }
    },
    name: '邮箱',
  },
  age: {
    rule: (rule, value, callback) => {
      const reg = /^[0-9]*[1-9][0-9]*$/;
      if (!(reg.test(value) || Number(value) === 0)) {
        callback('请输入大于等于0的整数');
      } else {
        callback();
      }
    },
    name: '年龄',
  },
  url: {
    rule: (rule, value, callback) => {
      const reg =
        /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
      if (!(reg.test(value) || Number(value) === 0)) {
        callback('请输入正确的URL');
      } else {
        callback();
      }
    },
    name: 'url',
  },
};

export const getValidators = () => {
  return Object.keys(formValidation).map((o) => {
    return {
      label: formValidation[o].name,
      value: o,
    };
  });
};
export default formValidation;
